<?php

class ProjectTopic extends CActiveRecord
{
	private static $cachedObjects = array();
	
	const YES = "y";
	const NO = "n";
	const INHERITS = "i";
	
	const STEP = "s";
	const TOPIC = "t";
	
	/**
	 * The followings are the available columns in table 'project_topic':
	 * @var integer $id
	 * @var string $label
	 * @var string $description
	 * @var string $key
	 * @var string $parent_key
	 * @var string $start_date
	 * @var string $end_date
	 * @var string $objective
	 * @var string $owner_validation
	 * @var string $adviser_validation
	 * @var string $validation_comment
	 * @var string $objective_needs
	 * @var string $objective_contacts
	 * @var string $adviser_validation_date
	 * @var string $owner_validation_date
	 * @var string $objective_date
	 * @var integer $project_id
	 * @var integer $topic_id
	 * @var integer $step_id
	 * @var string $item_type
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return ProjectTopic the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'project_topic';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('label, key, start_date, end_date, project_id, item_type', 'required'),
			array('project_id, topic_id, step_id', 'numerical', 'integerOnly'=>true),
			array('label', 'length', 'max'=>255),
			array('key, parent_key', 'length', 'max'=>16),
			array('objective, owner_validation, adviser_validation, item_type', 'length', 'max'=>1),
			array('objective, owner_validation, adviser_validation', 'validateCheckedValue'), 
			array('item_type', 'validateType'), 
			array('description, validation_comment, objective_needs, objective_contacts, adviser_validation_date, owner_validation_date, objective_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, label, description, key, parent_key, start_date, end_date, objective, owner_validation, adviser_validation, validation_comment, objective_needs, objective_contacts, adviser_validation_date, owner_validation_date, objective_date, project_id, topic_id, step_id, item_type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'project' => array (self::BELONGS_TO, 'Project', 'project_id'),
			//'topics' => array(self::HAS_MANY, 'ProjectTopic', 'parent_id', 'order' => 'idx'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('database', 'Id'),
			'label' => Yii::t('database', 'Label'),
			'description' => Yii::t('database', 'Description'),
			'key' => Yii::t('database', 'Key'),
			'parent_key' => Yii::t('database', 'Parent Key'),
			'start_date' => Yii::t('database', 'Start Date'),
			'end_date' => Yii::t('database', 'End Date'),
			'objective' => Yii::t('database', 'Objective'),
			'owner_validation' => Yii::t('database', 'Owner Validation'),
			'adviser_validation' => Yii::t('database', 'Adviser Validation'),
			'validation_comment' => Yii::t('database', 'Validation Comment'),
			'objective_needs' => Yii::t('database', 'Objective Needs'),
			'objective_contacts' => Yii::t('database', 'Objective Contacts'),
			'adviser_validation_date' => Yii::t('database', 'Adviser Validation Date'),
			'owner_validation_date' => Yii::t('database', 'Owner Validation Date'),
			'objective_date' => Yii::t('database', 'Objective Date'),
			'project_id' => Yii::t('database', 'Project'),
			'topic_id' => Yii::t('database', 'Topic'),
			'step_id' => Yii::t('database', 'Step'),
			'item_type' => Yii::t('database', 'Item Type'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('label',$this->label,true);

		$criteria->compare('description',$this->description,true);

		$criteria->compare('key',$this->key,true);

		$criteria->compare('parent_key',$this->parent_key,true);

		$criteria->compare('start_date',$this->start_date,true);

		$criteria->compare('end_date',$this->end_date,true);

		$criteria->compare('objective',$this->objective,true);

		$criteria->compare('owner_validation',$this->owner_validation,true);

		$criteria->compare('adviser_validation',$this->adviser_validation,true);

		$criteria->compare('validation_comment',$this->validation_comment,true);

		$criteria->compare('objective_needs',$this->objective_needs,true);

		$criteria->compare('objective_contacts',$this->objective_contacts,true);

		$criteria->compare('adviser_validation_date',$this->adviser_validation_date,true);

		$criteria->compare('owner_validation_date',$this->owner_validation_date,true);

		$criteria->compare('objective_date',$this->objective_date,true);

		$criteria->compare('project_id',$this->project_id);

		$criteria->compare('topic_id',$this->topic_id);

		$criteria->compare('step_id',$this->step_id);

		$criteria->compare('item_type',$this->item_type,true);

		return new CActiveDataProvider('ProjectTopic', array(
			'criteria'=>$criteria,
		));
	}
	
	function validateCheckedValue($attribute,$params) {
		$value = $this->$attribute;
		if ($value != ProjectTopic::YES && $value != ProjectTopic::NO && $value != ProjectTopic::INHERITS) {
			$this->addError($attribute,"Must be one of 'y', 'n', 'i'");
		}
	}
	
	function validateType($attribute,$params) {
		$value = $this->$attribute;
		if ($value != ProjectTopic::STEP && $value != ProjectTopic::TOPIC) {
			$this->addError($attribute,"Must be one of 's', 't'");
		}
	}
	
	private $_parent = null;
	private $_parent_set = false;
	
	function getParent() {
		if (!$this->_parent_set) {
			if ($this->parent_key != null)
				$this->_parent = ProjectTopic::model()->find('`key` = :key and project_id = :project_id', array(':key' => $this->parent_key, ':project_id' => $this->project_id));
			$this->_parent_set = true;
		}
		return $this->_parent;
	}
	
	function setParent(&$parent) {
		if ($this->getParent() != null) {
			$this->getParent()->removeChild($this);
		}
		if ($parent != null && $parent instanceof ProjectTopic){
			$parent->addChild($this);
		}
	}
	
	private $_children = null;
	
	function getChildren() {
		if ($this->_children == null) {
			$children = ProjectTopic::model()->findAll('parent_key = :key and project_id = :project_id', array(':key' => $this->key, ':project_id' => $this->project_id));
			$this->_children = array();
			if ($children != null) {
				foreach ($children as $child) {
					$this->_children[$child->key] = $child;
				}
			}
		}
		return $this->_children;
	}
	
	function setChildren($children) {
		$tmp = $this->getChildren();
		foreach ($tmp as $child) {
			$child->_parent = null;
			$child->parent_key = null;
		}
		
		if (!is_array($children)) {
			$children = array( $children );
		}
		
		if (is_array($children)) {
			foreach ($children as $child){
				if ($child instanceof ProjectTopic) {
					$child->parent_key = $this->key;
					$child->parent = $this;
				}
			}
		}
		
		$this->_children = children;
	}
	
	public function addChild(&$child) {
		$this->getChildren();
		if ($child->parent_key != $this->key || ! array_key_exists($child->key, $this->_children)) {
			$this->_children[$child->key] = $child;
			$child->parent_key = $this->key;
			$child->idx = count($this->_children);
			$child->_parent = $this;
		}
	}
		
	public function removeChild($child) {
		if ($child->parent_key == $this->key) {
			$this->getChildren();
			
			$idx = array_search($child, $this->_children);
			if ($idx !== false) {
				unset($this->_children[$idx]);
			}
			$child->parent_key = null;
			$child->parent = null;
		}
	}
	
	public function beforeSave() {
		$b = parent::beforeSave();
		
		if ($this->start_date != null && $this->start_date instanceof Date) {
			$this->start_date = $this->start_date->toMysqlDateString(); 
		}
		if ($this->end_date != null && $this->end_date instanceof Date) {
			$this->end_date = $this->end_date->toMysqlDateString(); 
		}
		
		return $b;
	}
	
	
	public function afterFind() {
		$b = parent::afterFind();
		
		$this->checked = 1;
		
		if ($this->start_date != null) {
			$this->start_date = new Date(strtotime($this->start_date));
		}
		
		if ($this->end_date != null) {
			$this->end_date = new Date(strtotime($this->end_date));
		}
		return $b;
	}
	
	public function afterSave() {
		parent::afterSave();
		
		if ($this->start_date != null) {
			$this->start_date = new Date(strtotime($this->start_date));
		}
		
		if ($this->end_date != null) {
			$this->end_date = new Date(strtotime($this->end_date));
		}
	}
	
	private $checked = 0;
	
	public function setChecked($checked) {
		$this->checked = ($checked ? 1 : 0);
	}
		
	public function getChecked() {
		return $this->checked;
	}
}